package com.jy.adscm.operation.mscyb.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.base.bean.JyCustomerProvider;
import com.jy.adscm.operation.base.bean.JyGoods;
import com.jy.adscm.operation.cg.bean.CgBom;
import com.jy.adscm.operation.cg.bean.CgMaterial;
import com.jy.adscm.operation.cg.bean.CgSupplier;
import com.jy.adscm.operation.mscyb.bean.JqbzPartsassembly;
import com.jy.adscm.operation.mscyb.bean.JqylMaterial;
import com.jy.adscm.operation.mscyb.bean.QueryJqylMaterial;
import com.jy.adscm.operation.mscyb.service.JqylMaterialService;
import com.jy.adscm.operation.purchase.bean.Purchasebom;
import com.jy.adscm.operation.purchase.bean.Supplier;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2018-11-29
 */
@RestController
@RequestMapping("/adscm/operation/jqylMaterial")
public class JqylMaterialController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    JqylMaterialService jqylMaterialService;
    @Autowired
    JyCodeMapper jyCodeMapper;
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody JqylMaterial jqylMaterial, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
       // jqylMaterial.setTaskcode( Long.valueOf(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) ));
        jqylMaterial.setTaskreleaseperson(token.getUser().getId().toString());
        jqylMaterial.setTaskreleaseaddtime(new Date());
        jqylMaterial.setTaskfinishstate(0);
        jqylMaterial.setTaskstate(0);
        jqylMaterial.setIstaskstart("0");
        jqylMaterial.setIstaskbegin("0");
        jqylMaterial.setIstaskend("0");
        jqylMaterial.setIsreviewtask("0");
        jqylMaterial.setReviewresult("-1");

        boolean insert = jqylMaterialService.insertJqylMaterial(jqylMaterial);
        return new SimpleCoreResponse("200","新增成功",insert);
    }

    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer id)throws AdscmSystemException {
        boolean result = jqylMaterialService.deleteById(id);
        return new  SimpleCoreResponse("200","删除成功",result);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody JqylMaterial jqylMaterial, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Boolean result = jqylMaterialService.updateJqylMaterialy(jqylMaterial,token);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("selectjqylMaterial")
    public  SimpleCoreResponse  selectjqylMaterial(@RequestBody QueryJqylMaterial queryJqylMaterial) throws  AdscmSystemException{
        Page<JqylMaterial> page =new Page<>(queryJqylMaterial.getCurrentPage(),queryJqylMaterial.getPageSize());
        page= jqylMaterialService.selectJqylMaterial(page,queryJqylMaterial);
        return  new SimpleCoreResponse("200","查询成功",page);
    }

    @RequestMapping("selectTaskPerformPerson")
    public  SimpleCoreResponse  selectTaskPerformPerson() throws  AdscmSystemException{
        List<JyUser> users = jqylMaterialService.selectTaskPerformPerson();
        return  new SimpleCoreResponse("200","查询成功",users);
    }
    @RequestMapping("selectJqylMaterialGoods")
    public  SimpleCoreResponse  selectJqylMaterialGoods() throws  AdscmSystemException{
        List<CgMaterial> cgMaterial = jqylMaterialService.selectJqylMaterialGoods();
        return  new SimpleCoreResponse("200","查询成功",cgMaterial);
    }
    @RequestMapping("selectJqylMaterialProvider/{goodsId}")
    public  SimpleCoreResponse  selectJqylMaterialProvider(@PathVariable String goodsId) throws  AdscmSystemException{
        List<CgSupplier> cgSuppliers = jqylMaterialService.selectJqylMaterialProvider(goodsId);
        return  new SimpleCoreResponse("200","查询成功",cgSuppliers);
    }
}

